﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Common;
using DataAccess;

public partial class Manager_EditCategory : System.Web.UI.Page
{
    string CategoryID = "";
    DataTable table;
    protected void Page_Init(object sender, EventArgs e)
    {
        if (Request.QueryString["ID"] != null && Request.QueryString["ID"] != "")
        {
            CategoryID = Request.QueryString["ID"];
            DbCommand cmd = GenericData.CreateCommand();
            cmd.CommandText = "SELECT * FROM Category WHERE ID = " + CategoryID;
            table = GenericData.ExecuteReader(cmd);
            if (table.Rows.Count > 0)
            {
                txtCategory.Text = table.Rows[0]["CategoryName"].ToString();
                txtDescription.Text = table.Rows[0]["Description"].ToString();
            }
            else
            {
                Response.Redirect("ManagerCategory.aspx");
            }
        }

    }

    protected void Page_Load(object sender, EventArgs e)
    {
        
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            //get data from form
            string CategoryName = txtCategory.Text;
            string Description = txtDescription.Text;

            Category cat = new Category();
            cat.CategoryName = CategoryName;
            cat.Description = Description;
            cat.ID = int.Parse(CategoryID);

            CategoryDAL catdal = new CategoryDAL();

            if (catdal.Update(cat))
                Session.Add("Message", "Update successfully CategoryID : " + CategoryID);
            else
                Session.Add("Message", "<font color='red'>Error : Update unsuccess CategoryID : " + CategoryID + "</font>");

            Response.Redirect("ManagerCategory.aspx");
        }
    }
}